<?php
declare (strict_types = 1);

namespace app\sk\tpl;
use app\core\Res;


class Layui extends Base
{
    public function jsIndex($params){
        $cols=Res::kv($params,'cols');
        $mtableurl=Res::kv($params,'mtableurl');
        $toolbar=Res::kv($params,'toolbar');
        $TitleEvent=Res::kv($params,'TitleEvent');
        $TbBar=Res::kv($params,'TbBar');
        $TbEvent=Res::kv($params,'TbEvent');
        $ExportUrl=Res::kv($params,'ExportUrl');
        $value=
<<<EOF
<script type="text/html" id="authTbBar">
    $TbBar
</script> 
<script>
    var mData='';

    function child(obj){
        mData = JSON.parse(obj);
    }
    
	var curId=0;
	//表格数据
	var tableData=[];
	
    $cols

	//Layui主运行函数
    layui.use(['form','admin','laydate','table','tableEdit','soulTable'], function () {
		 laydate = layui.laydate;
         table = layui.table;
         soulTable = layui.soulTable;
		 tableEdit = layui.tableEdit;
         form = layui.form;
         $ = layui.$;

	

		

		/* 渲染时间选择 */
        laydate.render({
            elem: 'input[name="createDateSel"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        // 保存

        

		

        
		
		//右键菜单配置
		var rightMenu = {
			// 表格内容右键菜单配置
			body: [
				{
				   name: '复制单元格内容',
				   icon: 'layui-icon layui-icon-app',
				   click: function(obj) {
					   soulTable.copy(obj.text);
					   layer.msg('复制成功！');
				   }
				},			
				{
					name: '高亮当前行',
					icon: 'layui-icon layui-icon-light',
					click: function(obj) {
						obj.trElem.css('color', '#ff0000');
						obj.trElem.css('background', '#fff071');
					}
				},
				{
					name: '文字标红',
					icon: 'layui-icon layui-icon-rate-half',
					click: function(obj) {
					  obj.elem.css('color', 'red');
					}
				}
			]
		};
		
		//表格列配置
			
		//创建表格
        myTable = table.render({
            elem: '#myTable'
            ,url: "$mtableurl"
			
			,toolbar: ['<p>',
                $toolbar
                '</p>'].join('')
            ,page: {limit: 20}
            ,limits: [20, 50, 100, 200, 500]
			,height: $(document).height() - $('#myTable').offset().top - 40
			,even: true //开启隔行背景
			,limit: 20 //分页记录条数
			,autoSort: false
			,contextmenu: rightMenu	//右键菜单	
            ,cols: [cols] //表格列的配置
			,filter: {  //开启表格筛选配置
				items:['column','clearCache'],
				bottom: false,
				cache: true
			}	
            ,done: function (res) { //表格创建完毕后的回调方法
                soulTable.render(this);

            },
			parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
			
        });

        /* 表格2头工具栏点击事件 */
        table.on('toolbar(myTable)', function (obj) {
            
            formvar=form.val('myForm');
			let params=formatDate(formvar);
			exportExcelUrl="$ExportUrl?"+params;
            $TitleEvent
        });

        

        /* 表格搜索 */
        form.on('submit(mxTbSearch)', function (data) {
            myTable.reload({where: data.field, page:1,limits:30});
            return false;
        });

        editTableCols = myTable.config.cols;
        var aopTable = tableEdit.aopObj(editTableCols); //获取一个aop对象

        aopTable.on('tool(myTable)',function (obj) {
            $TbEvent	
		
        });	

	
		
    });

	
</script>
</body>
</html>
EOF;
        return $value;
    }

    public function jsAdd($params){
        $laydate=Res::kv($params,'laydate');
        $addurl=Res::kv($params,'addurl');
        $value=
<<<EOF
<script>
    var mData='';

    function child(obj){
        mData = JSON.parse(obj);
    }
    
	var curId=0;
	//表格数据
	var tableData=[];
	
    

	//Layui主运行函数
    layui.use(['form','admin','laydate','table','tableEdit','soulTable'], function () {
		 laydate = layui.laydate;
         table = layui.table;
         soulTable = layui.soulTable;
		 tableEdit = layui.tableEdit;
         form = layui.form;
         $ = layui.$;

	
         form.on('submit(btnSave)', function (data) {
            // * 按钮禁用
            var isDisabled = $("#btnSave").hasClass('layui-btn-disabled');
            if (isDisabled) {
                return false;
            }
            // * 整合表单数据
            var formData = data.field;
            // * 确定路径
            var url = "$addurl";

            // * 发起请求

            $.ajax({

                data: formData,

                type: "POST",

                dataType: "JSON",

                url: url,

                beforeSend: function () {
                    // 禁用
                    $("#btnSave").addClass('layui-btn-disabled');
                },
                complete: function () {
                    // 启用
                    $("#btnSave").removeClass('layui-btn-disabled');
                },
                success: function (result) {
                    if (result.code==1) {  
                        layer.msg(result.msg, {icon: 1});
                        window.parent.location.reload();//刷新父页面
                        parent.layer.close(parent.layer.getFrameIndex(window.name));

                    } else {
                        // 提示失败
                        layer.msg(result.msg, {icon: 5, anim: 6});
                    }
                }
            }); // end ajax
            return false;

        });
		
        $laydate

		

	
		
    });

	
</script>
</body>
</html>
EOF;
        return $value;
    }

    public function jsEdit($params){
        $laydate=Res::kv($params,'laydate');
        $editurl=Res::kv($params,'editurl');
        $value=
<<<EOF
<script>
    var mData='';

    function child(obj){
        mData = JSON.parse(obj);
    }
    
	var curId=0;
	//表格数据
	var tableData=[];
	
    

	//Layui主运行函数
    layui.use(['form','admin','laydate','table','tableEdit','soulTable'], function () {
		 laydate = layui.laydate;
         table = layui.table;
         soulTable = layui.soulTable;
		 tableEdit = layui.tableEdit;
         form = layui.form;
         $ = layui.$;

         // 回显数据
			setTimeout(function(){
				form.val('myForm', mData);
			},500);

	
         form.on('submit(btnSave)', function (data) {
            // * 按钮禁用
            var isDisabled = $("#btnSave").hasClass('layui-btn-disabled');
            if (isDisabled) {
                return false;
            }
            // * 整合表单数据
            var formData = data.field;
            formData.id=mData.id
            // * 确定路径
            var url = "$editurl";

            // * 发起请求

            $.ajax({

                data: formData,

                type: "POST",

                dataType: "JSON",

                url: url,

                beforeSend: function () {
                    // 禁用
                    $("#btnSave").addClass('layui-btn-disabled');
                },
                complete: function () {
                    // 启用
                    $("#btnSave").removeClass('layui-btn-disabled');
                },
                success: function (result) {
                    if (result.code==1) {  
                        layer.msg(result.msg, {icon: 1});
                        window.parent.location.reload();//刷新父页面
                        parent.layer.close(parent.layer.getFrameIndex(window.name));

                    } else {
                        // 提示失败
                        layer.msg(result.msg, {icon: 5, anim: 6});
                    }
                }
            }); // end ajax
            return false;

        });
		
        $laydate

		

	
		
    });

	
</script>
</body>
</html>
EOF;
        return $value;
    }

}